package cn.fuxue.hrms.web.admin_controller;

import cn.fuxue.hrms.common.dto.RecruitInfoShow;
import cn.fuxue.hrms.common.entity.RecruitInfo;
import cn.fuxue.hrms.service.RecruitInfoService;
import cn.fuxue.hrms.service.impl.RecruitInfoServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * author:fuxue
 * description:后台 招聘管理 接口+视图解析
 */
@Controller
@RequestMapping("/admin/recruit")
public class RecruitInfoController {
    @Autowired
    private RecruitInfoServiceImpl recruitInfoService;

    /**
     * 展示所有
     * @param pageNum：第几页
     * @param pageSize：多少条
     * @param likename：搜索
     * @param model：视图模型容器
     * @return 视图
     */

    @GetMapping("/find")
    public String find(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "likename",defaultValue = "") String likename,
                       Model model) {
        List<RecruitInfoShow> recruitInfoList=recruitInfoService.findAll(likename);
        PageInfo<RecruitInfoShow> pageInfo=new PageInfo<>(recruitInfoList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/recruit";
    }

    @GetMapping("/find/{id}")
    public String findById(@PathVariable("id") Integer id, Model model) {
        RecruitInfo recruitInfo=recruitInfoService.selectByPrimaryKey(id);
        model.addAttribute("recruitInfo",recruitInfo);
        return "admin/recruitDetail";
    }


    @PostMapping("/update/")
    public String update(
            @RequestParam("id") Integer id,
            @RequestParam("title") String title,
            @RequestParam("needPeople") Integer needPeople,
            @RequestParam("endTime") String endTime,
            @RequestParam("salaryId") Integer salary_id
            ) throws ParseException {

        RecruitInfo recruitInfo=new RecruitInfo();
        if (id!=null && id!=0) {
            recruitInfo.setId(id);
        }
        if(title!=null && !title.equals("")) {
            recruitInfo.setTitle(title);
        }
        if(needPeople!=null && needPeople!=0) {
            recruitInfo.setNeedPeople(needPeople);
        }
        if (salary_id!=null && !salary_id.equals("")) {
            recruitInfo.setSalaryId(salary_id);
        }
        if (endTime!=null && !endTime.equals("")) {
            SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
            Date date = ft.parse(endTime);
            recruitInfo.setEndTime(date);
        }
        recruitInfoService.updateByPrimaryKeySelective(recruitInfo);
        return "redirect:/admin/recruit/find/"+id;
    }

}
